

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="python" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="python" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>QUANTAXIS.QASU.save_tushare &mdash; QUANTAXIS 1.0.39 documentation</title>
  

  
  
  
  

  

  
  
    

  

  
    <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 

  
  <script src="../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../index.html" class="icon icon-home"> QUANTAXIS
          

          
          </a>

          
            
            
              <div class="version">
                1.0.39
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <!-- Local TOC -->
              <div class="local-toc"></div>
            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">QUANTAXIS</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>QUANTAXIS.QASU.save_tushare</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for QUANTAXIS.QASU.save_tushare</h1><div class="highlight"><pre>
<span></span><span class="c1"># coding:utf-8</span>
<span class="c1">#</span>
<span class="c1"># The MIT License (MIT)</span>
<span class="c1">#</span>
<span class="c1"># Copyright (c) 2016-2018 yutiansut/QUANTAXIS</span>
<span class="c1">#</span>
<span class="c1"># Permission is hereby granted, free of charge, to any person obtaining a copy</span>
<span class="c1"># of this software and associated documentation files (the &quot;Software&quot;), to deal</span>
<span class="c1"># in the Software without restriction, including without limitation the rights</span>
<span class="c1"># to use, copy, modify, merge, publish, distribute, sublicense, and/or sell</span>
<span class="c1"># copies of the Software, and to permit persons to whom the Software is</span>
<span class="c1"># furnished to do so, subject to the following conditions:</span>
<span class="c1">#</span>
<span class="c1"># The above copyright notice and this permission notice shall be included in all</span>
<span class="c1"># copies or substantial portions of the Software.</span>
<span class="c1">#</span>
<span class="c1"># THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR</span>
<span class="c1"># IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</span>
<span class="c1"># FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE</span>
<span class="c1"># AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</span>
<span class="c1"># LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,</span>
<span class="c1"># OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE</span>
<span class="c1"># SOFTWARE.</span>

<span class="kn">import</span> <span class="nn">datetime</span>
<span class="kn">import</span> <span class="nn">json</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">import</span> <span class="nn">time</span>

<span class="kn">import</span> <span class="nn">tushare</span> <span class="k">as</span> <span class="nn">ts</span>

<span class="kn">from</span> <span class="nn">QUANTAXIS.QAFetch.QATushare</span> <span class="k">import</span> <span class="p">(</span><span class="n">QA_fetch_get_stock_day</span><span class="p">,</span>
                                         <span class="n">QA_fetch_get_stock_info</span><span class="p">,</span>
                                         <span class="n">QA_fetch_get_stock_list</span><span class="p">,</span>
                                         <span class="n">QA_fetch_get_trade_date</span><span class="p">,</span>
                                         <span class="n">QA_fetch_get_lhb</span><span class="p">)</span>
<span class="kn">from</span> <span class="nn">QUANTAXIS.QAUtil</span> <span class="k">import</span> <span class="p">(</span><span class="n">QA_util_date_stamp</span><span class="p">,</span> <span class="n">QA_util_log_info</span><span class="p">,</span>
                              <span class="n">QA_util_time_stamp</span><span class="p">,</span> <span class="n">QA_util_to_json_from_pandas</span><span class="p">,</span>
                              <span class="n">trade_date_sse</span><span class="p">)</span>
<span class="kn">from</span> <span class="nn">QUANTAXIS.QAUtil.QASetting</span> <span class="k">import</span> <span class="n">DATABASE</span>


<span class="kn">import</span> <span class="nn">tushare</span> <span class="k">as</span> <span class="nn">QATs</span>


<div class="viewcode-block" id="QA_save_stock_day_all"><a class="viewcode-back" href="../../../source/QUANTAXIS.QASU.html#QUANTAXIS.QASU.save_tushare.QA_save_stock_day_all">[docs]</a><span class="k">def</span> <span class="nf">QA_save_stock_day_all</span><span class="p">(</span><span class="n">client</span><span class="o">=</span><span class="n">DATABASE</span><span class="p">):</span>
    <span class="n">df</span> <span class="o">=</span> <span class="n">ts</span><span class="o">.</span><span class="n">get_stock_basics</span><span class="p">()</span>
    <span class="n">__coll</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">stock_day</span>
    <span class="n">__coll</span><span class="o">.</span><span class="n">ensure_index</span><span class="p">(</span><span class="s1">&#39;code&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">saving_work</span><span class="p">(</span><span class="n">i</span><span class="p">):</span>
        <span class="n">QA_util_log_info</span><span class="p">(</span><span class="s1">&#39;📝Now Saving ==== </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">i</span><span class="p">))</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">data_json</span> <span class="o">=</span> <span class="n">QA_fetch_get_stock_day</span><span class="p">(</span>
                <span class="n">i</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="s1">&#39;1990-01-01&#39;</span><span class="p">)</span>

            <span class="n">__coll</span><span class="o">.</span><span class="n">insert_many</span><span class="p">(</span><span class="n">data_json</span><span class="p">)</span>
        <span class="k">except</span><span class="p">:</span>
            <span class="n">QA_util_log_info</span><span class="p">(</span><span class="s1">&#39;error in saving ==== </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">))</span>

    <span class="k">for</span> <span class="n">i_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">index</span><span class="p">)):</span>
        <span class="n">QA_util_log_info</span><span class="p">(</span><span class="s1">&#39;The </span><span class="si">%s</span><span class="s1"> of Total </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">i_</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">index</span><span class="p">)))</span>
        <span class="n">QA_util_log_info</span><span class="p">(</span><span class="s1">&#39;⏳DOWNLOAD PROGRESS </span><span class="si">%s</span><span class="s1"> &#39;</span> <span class="o">%</span> <span class="nb">str</span><span class="p">(</span>
            <span class="nb">float</span><span class="p">(</span><span class="n">i_</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">index</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100</span><span class="p">))[</span><span class="mi">0</span><span class="p">:</span><span class="mi">4</span><span class="p">]</span> <span class="o">+</span> <span class="s1">&#39;%&#39;</span><span class="p">)</span>
        <span class="n">saving_work</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">index</span><span class="p">[</span><span class="n">i_</span><span class="p">])</span>

    <span class="n">saving_work</span><span class="p">(</span><span class="s1">&#39;hs300&#39;</span><span class="p">)</span>
    <span class="n">saving_work</span><span class="p">(</span><span class="s1">&#39;sz50&#39;</span><span class="p">)</span></div>


<div class="viewcode-block" id="QA_SU_save_stock_list"><a class="viewcode-back" href="../../../source/QUANTAXIS.QASU.html#QUANTAXIS.QASU.save_tushare.QA_SU_save_stock_list">[docs]</a><span class="k">def</span> <span class="nf">QA_SU_save_stock_list</span><span class="p">(</span><span class="n">client</span><span class="o">=</span><span class="n">DATABASE</span><span class="p">):</span>
    <span class="n">data</span> <span class="o">=</span> <span class="n">QA_fetch_get_stock_list</span><span class="p">()</span>
    <span class="n">date</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">today</span><span class="p">())</span>
    <span class="n">date_stamp</span> <span class="o">=</span> <span class="n">QA_util_date_stamp</span><span class="p">(</span><span class="n">date</span><span class="p">)</span>
    <span class="n">coll</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">stock_info_tushare</span>
    <span class="n">coll</span><span class="o">.</span><span class="n">insert</span><span class="p">({</span><span class="s1">&#39;date&#39;</span><span class="p">:</span> <span class="n">date</span><span class="p">,</span> <span class="s1">&#39;date_stamp&#39;</span><span class="p">:</span> <span class="n">date_stamp</span><span class="p">,</span>
                 <span class="s1">&#39;stock&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;code&#39;</span><span class="p">:</span> <span class="n">data</span><span class="p">}})</span></div>


<div class="viewcode-block" id="QA_SU_save_stock_info_tushare"><a class="viewcode-back" href="../../../source/QUANTAXIS.QASU.html#QUANTAXIS.QASU.save_tushare.QA_SU_save_stock_info_tushare">[docs]</a><span class="k">def</span> <span class="nf">QA_SU_save_stock_info_tushare</span><span class="p">(</span><span class="n">client</span><span class="o">=</span><span class="n">DATABASE</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        获取 股票的 基本信息，包含股票的如下信息</span>

<span class="sd">        code,代码</span>
<span class="sd">        name,名称</span>
<span class="sd">        industry,所属行业</span>
<span class="sd">        area,地区</span>
<span class="sd">        pe,市盈率</span>
<span class="sd">        outstanding,流通股本(亿)</span>
<span class="sd">        totals,总股本(亿)</span>
<span class="sd">        totalAssets,总资产(万)</span>
<span class="sd">        liquidAssets,流动资产</span>
<span class="sd">        fixedAssets,固定资产</span>
<span class="sd">        reserved,公积金</span>
<span class="sd">        reservedPerShare,每股公积金</span>
<span class="sd">        esp,每股收益</span>
<span class="sd">        bvps,每股净资</span>
<span class="sd">        pb,市净率</span>
<span class="sd">        timeToMarket,上市日期</span>
<span class="sd">        undp,未分利润</span>
<span class="sd">        perundp, 每股未分配</span>
<span class="sd">        rev,收入同比(%)</span>
<span class="sd">        profit,利润同比(%)</span>
<span class="sd">        gpr,毛利率(%)</span>
<span class="sd">        npr,净利润率(%)</span>
<span class="sd">        holders,股东人数</span>

<span class="sd">        add by tauruswang</span>

<span class="sd">    在命令行工具 quantaxis 中输入 save stock_info_tushare 中的命令</span>
<span class="sd">    :param client:</span>
<span class="sd">    :return:</span>
<span class="sd">    &#39;&#39;&#39;</span>
    <span class="n">df</span> <span class="o">=</span> <span class="n">QATs</span><span class="o">.</span><span class="n">get_stock_basics</span><span class="p">()</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;📡 Get stock info from tushare,stock count is </span><span class="si">%d</span><span class="s2">&quot;</span><span class="o">%</span> <span class="nb">len</span><span class="p">(</span><span class="n">df</span><span class="p">))</span>
    <span class="n">coll</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">stock_info_tushare</span>
    <span class="n">client</span><span class="o">.</span><span class="n">drop_collection</span><span class="p">(</span><span class="n">coll</span><span class="p">)</span>
    <span class="n">json_data</span> <span class="o">=</span>  <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">reset_index</span><span class="p">()</span><span class="o">.</span><span class="n">to_json</span><span class="p">(</span><span class="n">orient</span><span class="o">=</span><span class="s1">&#39;records&#39;</span><span class="p">))</span>
    <span class="n">coll</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="n">json_data</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;📝 Save data to stock_info_tushare collection， OK✅&quot;</span><span class="p">)</span></div>



<div class="viewcode-block" id="QA_SU_save_trade_date_all"><a class="viewcode-back" href="../../../source/QUANTAXIS.QASU.html#QUANTAXIS.QASU.save_tushare.QA_SU_save_trade_date_all">[docs]</a><span class="k">def</span> <span class="nf">QA_SU_save_trade_date_all</span><span class="p">(</span><span class="n">client</span><span class="o">=</span><span class="n">DATABASE</span><span class="p">):</span>
    <span class="n">data</span> <span class="o">=</span> <span class="n">QA_fetch_get_trade_date</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
    <span class="n">coll</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">trade_date</span>
    <span class="n">coll</span><span class="o">.</span><span class="n">insert_many</span><span class="p">(</span><span class="n">data</span><span class="p">)</span></div>


<div class="viewcode-block" id="QA_SU_save_stock_info"><a class="viewcode-back" href="../../../source/QUANTAXIS.QASU.html#QUANTAXIS.QASU.save_tushare.QA_SU_save_stock_info">[docs]</a><span class="k">def</span> <span class="nf">QA_SU_save_stock_info</span><span class="p">(</span><span class="n">client</span><span class="o">=</span><span class="n">DATABASE</span><span class="p">):</span>
    <span class="n">data</span> <span class="o">=</span> <span class="n">QA_fetch_get_stock_info</span><span class="p">(</span><span class="s1">&#39;all&#39;</span><span class="p">)</span>
    <span class="n">coll</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">stock_info</span>
    <span class="n">coll</span><span class="o">.</span><span class="n">insert_many</span><span class="p">(</span><span class="n">data</span><span class="p">)</span></div>


<div class="viewcode-block" id="QA_save_stock_day_all_bfq"><a class="viewcode-back" href="../../../source/QUANTAXIS.QASU.html#QUANTAXIS.QASU.save_tushare.QA_save_stock_day_all_bfq">[docs]</a><span class="k">def</span> <span class="nf">QA_save_stock_day_all_bfq</span><span class="p">(</span><span class="n">client</span><span class="o">=</span><span class="n">DATABASE</span><span class="p">):</span>
    <span class="n">df</span> <span class="o">=</span> <span class="n">ts</span><span class="o">.</span><span class="n">get_stock_basics</span><span class="p">()</span>

    <span class="n">__coll</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">stock_day_bfq</span>
    <span class="n">__coll</span><span class="o">.</span><span class="n">ensure_index</span><span class="p">(</span><span class="s1">&#39;code&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">saving_work</span><span class="p">(</span><span class="n">i</span><span class="p">):</span>
        <span class="n">QA_util_log_info</span><span class="p">(</span><span class="s1">&#39;📝Now Saving ==== </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">i</span><span class="p">))</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">data_json</span> <span class="o">=</span> <span class="n">QA_fetch_get_stock_day</span><span class="p">(</span>
                <span class="n">i</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="s1">&#39;1990-01-01&#39;</span><span class="p">,</span> <span class="n">if_fq</span><span class="o">=</span><span class="s1">&#39;00&#39;</span><span class="p">)</span>

            <span class="n">__coll</span><span class="o">.</span><span class="n">insert_many</span><span class="p">(</span><span class="n">data_json</span><span class="p">)</span>
        <span class="k">except</span><span class="p">:</span>
            <span class="n">QA_util_log_info</span><span class="p">(</span><span class="s1">&#39;error in saving ==== </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">))</span>

    <span class="k">for</span> <span class="n">i_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">index</span><span class="p">)):</span>
        <span class="n">QA_util_log_info</span><span class="p">(</span><span class="s1">&#39;The </span><span class="si">%s</span><span class="s1"> of Total </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">i_</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">index</span><span class="p">)))</span>
        <span class="n">QA_util_log_info</span><span class="p">(</span><span class="s1">&#39;⏳DOWNLOAD PROGRESS </span><span class="si">%s</span><span class="s1"> &#39;</span> <span class="o">%</span> <span class="nb">str</span><span class="p">(</span>
            <span class="nb">float</span><span class="p">(</span><span class="n">i_</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">index</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100</span><span class="p">))[</span><span class="mi">0</span><span class="p">:</span><span class="mi">4</span><span class="p">]</span> <span class="o">+</span> <span class="s1">&#39;%&#39;</span><span class="p">)</span>
        <span class="n">saving_work</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">index</span><span class="p">[</span><span class="n">i_</span><span class="p">])</span>

    <span class="n">saving_work</span><span class="p">(</span><span class="s1">&#39;hs300&#39;</span><span class="p">)</span>
    <span class="n">saving_work</span><span class="p">(</span><span class="s1">&#39;sz50&#39;</span><span class="p">)</span></div>


<div class="viewcode-block" id="QA_save_stock_day_with_fqfactor"><a class="viewcode-back" href="../../../source/QUANTAXIS.QASU.html#QUANTAXIS.QASU.save_tushare.QA_save_stock_day_with_fqfactor">[docs]</a><span class="k">def</span> <span class="nf">QA_save_stock_day_with_fqfactor</span><span class="p">(</span><span class="n">client</span><span class="o">=</span><span class="n">DATABASE</span><span class="p">):</span>
    <span class="n">df</span> <span class="o">=</span> <span class="n">ts</span><span class="o">.</span><span class="n">get_stock_basics</span><span class="p">()</span>

    <span class="n">__coll</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">stock_day</span>
    <span class="n">__coll</span><span class="o">.</span><span class="n">ensure_index</span><span class="p">(</span><span class="s1">&#39;code&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">saving_work</span><span class="p">(</span><span class="n">i</span><span class="p">):</span>
        <span class="n">QA_util_log_info</span><span class="p">(</span><span class="s1">&#39;📝Now Saving ==== </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">i</span><span class="p">))</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">data_hfq</span> <span class="o">=</span> <span class="n">QA_fetch_get_stock_day</span><span class="p">(</span>
                <span class="n">i</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="s1">&#39;1990-01-01&#39;</span><span class="p">,</span> <span class="n">if_fq</span><span class="o">=</span><span class="s1">&#39;02&#39;</span><span class="p">,</span> <span class="n">type_</span><span class="o">=</span><span class="s1">&#39;pd&#39;</span><span class="p">)</span>
            <span class="n">data_json</span> <span class="o">=</span> <span class="n">QA_util_to_json_from_pandas</span><span class="p">(</span><span class="n">data_hfq</span><span class="p">)</span>
            <span class="n">__coll</span><span class="o">.</span><span class="n">insert_many</span><span class="p">(</span><span class="n">data_json</span><span class="p">)</span>
        <span class="k">except</span><span class="p">:</span>
            <span class="n">QA_util_log_info</span><span class="p">(</span><span class="s1">&#39;error in saving ==== </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">))</span>
    <span class="k">for</span> <span class="n">i_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">index</span><span class="p">)):</span>
        <span class="n">QA_util_log_info</span><span class="p">(</span><span class="s1">&#39;The </span><span class="si">%s</span><span class="s1"> of Total </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">i_</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">index</span><span class="p">)))</span>
        <span class="n">QA_util_log_info</span><span class="p">(</span><span class="s1">&#39;⏳DOWNLOAD PROGRESS </span><span class="si">%s</span><span class="s1"> &#39;</span> <span class="o">%</span> <span class="nb">str</span><span class="p">(</span>
            <span class="nb">float</span><span class="p">(</span><span class="n">i_</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">index</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100</span><span class="p">))[</span><span class="mi">0</span><span class="p">:</span><span class="mi">4</span><span class="p">]</span> <span class="o">+</span> <span class="s1">&#39;%&#39;</span><span class="p">)</span>
        <span class="n">saving_work</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">index</span><span class="p">[</span><span class="n">i_</span><span class="p">])</span>

    <span class="n">saving_work</span><span class="p">(</span><span class="s1">&#39;hs300&#39;</span><span class="p">)</span>
    <span class="n">saving_work</span><span class="p">(</span><span class="s1">&#39;sz50&#39;</span><span class="p">)</span>

    <span class="n">QA_util_log_info</span><span class="p">(</span><span class="s1">&#39;Saving Process has been done !&#39;</span><span class="p">)</span>
    <span class="k">return</span> <span class="mi">0</span></div>

<div class="viewcode-block" id="QA_save_lhb"><a class="viewcode-back" href="../../../source/QUANTAXIS.QASU.html#QUANTAXIS.QASU.save_tushare.QA_save_lhb">[docs]</a><span class="k">def</span> <span class="nf">QA_save_lhb</span><span class="p">(</span><span class="n">client</span><span class="o">=</span><span class="n">DATABASE</span><span class="p">):</span>
    <span class="n">__coll</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">lhb</span>
    <span class="n">__coll</span><span class="o">.</span><span class="n">ensure_index</span><span class="p">(</span><span class="s1">&#39;code&#39;</span><span class="p">)</span>

    <span class="n">start</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">strptime</span><span class="p">(</span><span class="s2">&quot;2006-07-01&quot;</span><span class="p">,</span> <span class="s2">&quot;%Y-%m-</span><span class="si">%d</span><span class="s2">&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">date</span><span class="p">()</span>
    <span class="n">end</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">today</span><span class="p">();</span>
    <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="k">while</span> <span class="n">start</span> <span class="o">&lt;</span> <span class="n">end</span><span class="p">:</span>
        <span class="n">i</span> <span class="o">=</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span>
        <span class="n">start</span> <span class="o">=</span> <span class="n">start</span> <span class="o">+</span> <span class="n">datetime</span><span class="o">.</span><span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="mi">1</span><span class="p">);</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">pd</span> <span class="o">=</span> <span class="n">QA_fetch_get_lhb</span><span class="p">(</span><span class="n">start</span><span class="o">.</span><span class="n">isoformat</span><span class="p">())</span>
            <span class="k">if</span> <span class="n">pd</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">continue</span><span class="p">;</span>
            <span class="n">data</span><span class="o">=</span>   <span class="n">pd</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">pchange</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">pchange</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="nb">float</span><span class="p">))</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">amount</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">amount</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="nb">float</span><span class="p">))</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">bratio</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">bratio</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="nb">float</span><span class="p">))</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">sratio</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">sratio</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="nb">float</span><span class="p">))</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">buy</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">buy</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="nb">float</span><span class="p">))</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">sell</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">sell</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="nb">float</span><span class="p">))</span>
            <span class="c1"># __coll.insert_many(QA_util_to_json_from_pandas(data))</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)):</span>
             <span class="n">__coll</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="s2">&quot;code&quot;</span><span class="p">:</span><span class="n">data</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;code&#39;</span><span class="p">],</span><span class="s2">&quot;date&quot;</span><span class="p">:</span><span class="n">data</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;date&#39;</span><span class="p">]},{</span><span class="s2">&quot;$set&quot;</span><span class="p">:</span><span class="n">QA_util_to_json_from_pandas</span><span class="p">(</span><span class="n">data</span><span class="p">)[</span><span class="n">i</span><span class="p">]},</span><span class="n">upsert</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">i</span> <span class="o">%</span> <span class="mi">10</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">60</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;error codes:&quot;</span><span class="p">)</span>
            <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
            <span class="k">continue</span></div>
<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="n">QA_save_lhb</span><span class="p">()</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, yutiansut.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../',
            VERSION:'1.0.39',
            LANGUAGE:'python',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../_static/doctools.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

  

  
  
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>